/**
 * UpdateTree.cpp
 * Coyright (c) 2009 Xiaofeng Gu. All Rights Reserved.
 * Author : Xiaofeng Gu
 *
 * Creation Data : 2009/07/17
 */

#include "stdafx.h"
#include "UpdateTree.h"
#include "Global.h"
#include "TFTreeView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

HRESULT CUpdateTree::OnExecute()
{
	HRESULT hr = S_OK;
	hr = __super::OnExecute();
	if (hr != S_OK)
	{
		return hr;
	}

	CTreeCtrl& tree = CSKGlobal::GetTreeView()->GetTreeCtrl();
	m_pTree = &tree;
	m_pTree->DeleteAllItems();

	DWORD treeStyle = TVS_HASBUTTONS | TVS_LINESATROOT | TVS_HASLINES | TVS_DISABLEDRAGDROP;
	m_pTree->ModifyStyle(NULL, treeStyle);

	CImageList* pImageList = CSKGlobal::GetTreeView()->GetImageList();
	if (m_pTree->GetImageList(TVSIL_NORMAL) == NULL)
	{
		pImageList->Create(IDB_TREE, 20, 1, RGB(255, 255, 255));
		m_pTree->SetImageList(pImageList, TVSIL_NORMAL);
	}

	CSKInfo* pSKInfo = GetBlockInfo<CSKInfo>(SKINFO_INSTANCE);
	
	CTimeline* pTimeline = pSKInfo->GetTimeline();

	HTREEITEM hItem;
	hItem = m_pTree->InsertItem(pTimeline->GetName(), 2, 2);

	Families* pFamiles = pTimeline->GetFamilies();
	for (Families::iterator itFa = pFamiles->begin(); itFa != pFamiles->end(); itFa ++)
	{
		CFamily* pFamily = *itFa;
		Initialize(hItem, pFamily);
	}

	return S_OK;
}

void CUpdateTree::Initialize(HTREEITEM hParentItem, CFamily* pFamily)
{
	HTREEITEM hItem;
	CString strFamily;
	strFamily.Format(_T("%s : %d"), pFamily->GetName(), 0);
	if (pFamily->GetActive())
	{
		hItem = m_pTree->InsertItem(strFamily, 2, 2, hParentItem, TVI_LAST);
	}
	else
	{
		hItem = m_pTree->InsertItem(strFamily, 5, 5, hParentItem, TVI_LAST);
	}
	pFamily->SetTreeItem(hItem);
	CSKObject* pSKObj = pFamily;
	m_pTree->SetItemData(hItem, (DWORD_PTR)pSKObj);

	Initialize(hItem, pFamily->GetKnots());

	pFamily->Expand(m_pTree);
}

void CUpdateTree::Initialize(HTREEITEM hParentItem, Knots* pKnots)
{
	CString strKnot;
	int i = 0;
	HTREEITEM hItem;
	for (Knots::iterator it = pKnots->begin();
		it != pKnots->end(); it ++, i ++)
	{
		CKnot* pKnot = (*it);
		strKnot.Format(_T("%s : %d"), pKnot->GetName(), pKnot->GetTime());
		if (pKnot->GetActive())
		{
			hItem = m_pTree->InsertItem(strKnot, 0, 0, hParentItem, TVI_LAST);
		}
		else
		{
			hItem = m_pTree->InsertItem(strKnot, 3, 3, hParentItem, TVI_LAST);
		}
		pKnot->SetTreeItem(hItem);
		CSKObject* pSKObj = pKnot;
		m_pTree->SetItemData(hItem, (DWORD_PTR)pSKObj);
		Initialize(pKnot, m_pTree, hItem);
	}
}

void CUpdateTree::Initialize(CKnot* pKnot, CTreeCtrl* pTree, HTREEITEM hItem)
{
	CTransNode* pTopNode = pKnot->GetTopNode();
	AddNode(pTree, pTopNode, hItem);
}

void CUpdateTree::AddNode(CTreeCtrl* pTree, CTransNode* pNode, HTREEITEM hParentItem)
{
	CString strNodeName = pNode->GetName();
	HTREEITEM hItem;
	if (pNode->GetActive())
	{
		hItem = pTree->InsertItem(strNodeName, 1, 1, hParentItem, TVI_LAST);
	}
	else
	{
		hItem = pTree->InsertItem(strNodeName, 4, 4, hParentItem, TVI_LAST);
	}
	pNode->SetTreeItem(hItem);
	CSKObject* pSKObj = pNode;
	pTree->SetItemData(hItem, (DWORD_PTR)pSKObj);

	Nodes* pNodes = pNode->GetChildren();
	if (pNodes->size() > 0)
	{
		for (Nodes::iterator it = pNodes->begin(); it != pNodes->end(); it ++)
		{
			AddNode(pTree, *it, hItem);
		}
	}
}